import html2canvas from 'html2canvas';
import {MutableRefObject} from "react";
const useDomToImage = (screenshotRef: MutableRefObject<HTMLElement | null>) => {
    const domImage = async () => {
        if (screenshotRef.current) {
            const canvas = await html2canvas(screenshotRef.current as HTMLElement);
            const base64String = canvas.toDataURL('image/png');
            // 去除数据URL的前缀部分，只保留Base64编码的数据
            const base64Data = base64String.split(',')[1];
            // 创建一个<a>标签用于下载
            const link = document.createElement('a');
            link.download = `${(new Date()).getTime()}.png`; // 指定下载文件的名称
            link.href = 'data:image/png;base64,' + base64Data; // 创建数据URL
            link.click(); // 触发下载
        }
    }
    return { domImage }
}

export default useDomToImage